import tkinter as tk
import random
import time

# 降低各奖项的概率
prizes = [
    ("特等奖20元", 1),
    ("一等奖10元", 2),
    ("二等奖6元", 5),
    ("三等奖4元", 10),
    ("幸运奖2元", 15),
    ("谢谢惠顾", 67)  # 注意这里的概率之和应该为100
]

# 初始化抽奖次数计数器和总奖金数
draw_count = 0
total_prize_money = 0

def spin_wheel():
    rand_num = random.randint(1, 100)
    cumulative_prob = 0
    for prize, prob in prizes:
        cumulative_prob += prob
        if rand_num <= cumulative_prob:
            return prize

def spin_button_clicked():
    global draw_count, total_prize_money  # 使用全局变量来跟踪抽奖次数和总奖金数
    draw_count += 1  # 每次点击抽奖按钮时递增抽奖次数

    # 更新抽奖次数标签
    draw_count_label.config(text=f"抽奖次数：{draw_count}")

    spin_button.config(state=tk.DISABLED)  # 禁用抽奖按钮
    result_label.config(text="抽奖中...")
    result_label.update_idletasks()

    result = spin_wheel()

    if result != "谢谢惠顾":
        show_special_effect(result)
        # 从奖项中获取奖金数额并累加到总奖金数
        prize_money = int(result.split('元')[0])  # 提取奖金数额
        total_prize_money += prize_money

    time.sleep(1)
    result_label.config(text=f"抽奖结果：{result}")
    spin_button.config(state=tk.NORMAL)  # 启用抽奖按钮

    # 更新总奖金数标签
    total_prize_label.config(text=f"总奖金：{total_prize_money}元")

def show_special_effect(prize):
    # 创建特效，可以根据奖项的不同调整特效
    for i in range(5):
        result_label.config(fg=random_color())
        result_label.update_idletasks()
        time.sleep(0.2)
        result_label.config(fg="black")
        result_label.update_idletasks()
        time.sleep(0.2)

def random_color():
    return "#{:02x}{:02x}{:02x}".format(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))


def spin_button_clicked():
    global draw_count, total_prize_money
    draw_count += 1
    draw_count_label.config(text=f"抽奖次数：{draw_count}")
    spin_button.config(state=tk.DISABLED)
    result_label.config(text="抽奖中...")
    result_label.update_idletasks()

    result = spin_wheel()

    if result != "谢谢惠顾":
        show_special_effect(result)
        # 添加条件来检查抽奖结果是否包含奖金数额
        if any(char.isdigit() for char in result):
            prize_money = int(''.join(filter(str.isdigit, result)))  # 提取奖金数额
            total_prize_money += prize_money

    time.sleep(1)
    result_label.config(text=f"抽奖结果：{result}")
    spin_button.config(state=tk.NORMAL)
    total_prize_label.config(text=f"总奖金：{total_prize_money}元")


# 创建主窗口
root = tk.Tk()
root.title("大转盘抽奖")

# 创建抽奖次数标签
draw_count_label = tk.Label(root, text="抽奖次数：0", font=("Helvetica", 12))
draw_count_label.pack(pady=10)

# 创建抽奖结果标签
result_label = tk.Label(root, text="抽奖结果：", font=("Helvetica", 16))
result_label.pack(pady=20)

# 创建总奖金数标签
total_prize_label = tk.Label(root, text="总奖金：0元", font=("Helvetica", 12))
total_prize_label.pack(pady=10)

# 创建抽奖按钮
spin_button = tk.Button(root, text="开始抽奖", command=spin_button_clicked)
spin_button.pack()

# 运行主循环
root.mainloop()
